/*
Copyright  2018 Blockchain Technology and Application Joint Lab, Fintech Research Center of ISCAS.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
import {Mongo} from 'meteor/mongo';
import {SimpleSchema} from 'meteor/aldeed:simple-schema';
import {Meteor} from 'meteor/meteor';
//define user fields
class UsersCollection extends Mongo.Collection {
    insert(doc, callback) {
        const ourDoc = doc;
        ourDoc.createdAt = ourDoc.createdAt || new Date();
        if (super.find({phone: ourDoc.phone}).count() > 0)
            throw new Meteor.Error('用户已存在');
        const result = super.insert(ourDoc, callback);
        return result;
    }

    update(selector, modifier) {
        const result = super.update(selector, modifier);
        return result;
    }

    remove(selector) {
        const result = super.remove(selector);
        return result;
    }
}

export const Users = new UsersCollection('users_crbb');

// Deny all client-side updates since we will be using methods to manage this collection
Users.deny({
    insert() {
        return true;
    },
    update() {
        return true;
    },
    remove() {
        return true;
    },
});

Users.schema = new SimpleSchema({
    phone: {// As user account id
        type: String,
    },
    prv_key_pem_encrypted: { // Users' pem format private key encrypted with some password
        // When user login, need the password to decrypt this filed
        type: String,
    },
    user_certificate_pem: { // Users' pem format certificate which needed to be sent to a repchain node
        // Which can register the user into repchain
        type: String,

    },
    address: {
        type: String,
    },
    unit: {
        type: String,
        optional: true,
    },
    account_address: {
        type: String,
        defaultValue: 'none',
    },
    credential_type: {
        type: Number,
        optional: true,
    },
    credential_number: {
        type: String,
        max: 100,
        optional: true,
    },
    name: {
        type: String,
        max: 100,
    },
    email: {
        type: String,
        max: 100,
    },
    createdAt: {
        type: Date,
    },
    status: {
        type: Boolean,
        defaultValue: false,
    },
    avatar: {
        type: String,
        optional: true,
    },
    isAdmin: {
        type: Boolean,
        defaultValue: false,
    },
});

Users.attachSchema(Users.schema);

Users.publicFields = {
    account_address: 1,
    name: 1,
};




if (Meteor.isServer) {
    // This code only runs on the server
    Meteor.publish('users', function usersPublication(uname) {
        let users = Users.find({name: {$regex: uname}}, {fields: Users.publicFields});
        return users;
    });

    Meteor.publish('user', function authorPublication(userId) {
        let query = {account_address: userId};
        return Users.find(query, {});
    });

    Meteor.publish('allUsers', function findAll() {
        return Users.find();
    });
}